package com.hsc.www.github;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

import javax.sql.DataSource;

@Configuration
@EnableAuthorizationServer
public class MyAuthorizationServerConfigurer implements AuthorizationServerConfigurer {

//    @Autowired
//    private AuthenticationManager authenticationManager;
//
    @Autowired
    private DataSource dataSource;


    @Bean
    public TokenStore tokenStore(){
        return new JdbcTokenStore(dataSource);
    }



    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //clients.inMemory()这种方式是将client在内存中维护，这里配置在数据库中维护
        clients.jdbc(dataSource);
    }

    @Override // 配置框架应用上述实现
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

//        endpoints.authenticationManager(authenticationManager);
        endpoints.tokenStore(tokenStore());
        //生成的授权码，默认是存放在内存中的，这里配置存放在数据库中
        endpoints.authorizationCodeServices(new JdbcAuthorizationCodeServices(dataSource));
        //endpoints.userDetailsService(userDetailsService);


    }




    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    }
}